'use strict';

/**
 * @ngdoc function
 * @name webApp.controller:RegisteredCtrl
 * @description
 * # RegisteredCtrl
 * Controller of the webApp
 */

angular.module('webApp')
  .controller('registered', function ($scope,promiseRegistered,popBox,$location,cookie,$localStorage) {
    $scope.$on('$stateChangeSuccess',function(){
      $scope.user = {
        email: '',
        password: '',
        password_confirmation: ''
      };
    });
    //单选按钮
    $scope.isChecked = false;
    $scope.change = function(){
      $scope.isChecked = !$scope.isChecked;
    };
    $scope.submit = function(){
      if($scope.isChecked){
        if($scope.user.email == '' && $scope.user.password == '' && $scope.user.password_confirmation == ''){
          popBox.showConfirm('警告','用户数据不能为空！');
        }
        else if($scope.user.password != $scope.user.password_confirmation){
          popBox.showConfirm('密码两次输入不一致！');
        }
        else{
          promiseRegistered.registered($scope.user).then(function(){
            popBox.showConfirm('提示','注册成功！');
            promiseRegistered.signIn($scope.user).then(function(data){
            	 $localStorage.access_token = data.access_token;
              cookie.setCookie('access_token',data.access_token,'s' + data.expires_in);
              $location.path('/root/home');
            });
          });
        }
      }
      else{
        popBox.showConfirm('警告','请先同意本网站条款！');
      }
    };
});
